package com.hymjweb.demo.hymj.customer_manage.freight_agreement.auto.mapper;

import com.hymjweb.demo.framework.util.DataTypeUtil;
import com.hymjweb.demo.hymj.customer_manage.freight_agreement.auto.bean.BCustomerAutoFreightAgreementPojo;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Mapper
@Component
public interface BCustomerAutoFreightAgreementMapper {

    class BCustomerAutoFreightAgreementMapperProvider{

        public String findAll(Map map){

            String sql= new SQL() {{
                SELECT("t.*");
                FROM("b_customer_auto_freight_agreement t ");
                WHERE("1=1");
            }}.toString();

            if(map!=null){
                /**
                 * let keyDeliveryCustomerId = mini.get("keyDeliveryCustomerId");
                 *             let keyCityId = mini.get("keyCityId");
                 */

                String keyDeliveryCustomerId = DataTypeUtil.toStringObject(map.get("keyDeliveryCustomerId"));
                String keyCityId = DataTypeUtil.toStringObject(map.get("keyCityId"));

                if(keyDeliveryCustomerId != null){
                    sql += " and t.customer_id = " + DataTypeUtil.quoteStr(keyDeliveryCustomerId);
                }

                if(keyCityId != null){
                    sql += " and t.city_id = " + DataTypeUtil.quoteStr(keyCityId);
                }
            }

            return sql;
        }

        public String getAutoFreightAgreementPojoByIds(String ids){
            String sql= new SQL() {{
                SELECT("t.*");
                FROM("b_customer_auto_freight_agreement t ");
                WHERE("1=1 and t.id in (" + ids + ")");
            }}.toString();
            return sql;
        }

        public String getAutoFreightAgreementPojo(BCustomerAutoFreightAgreementPojo autoAgreementPojo){
            String sql= new SQL() {{
                SELECT("t.*");
                FROM("b_customer_auto_freight_agreement t ");
                WHERE("1=1 and t.customer_id = #{customerId} and t.city_id = #{cityId}");
            }}.toString();
            return sql;
        }


        public String insert(BCustomerAutoFreightAgreementPojo autoAgreementPojo){
            String sql = new SQL() {{
                INSERT_INTO("b_customer_auto_freight_agreement");
                VALUES("id","#{id}");
                VALUES("customer_id","#{customerId}");
                VALUES("city_id","#{cityId}");
                VALUES("file_id","#{fileId}");
                VALUES("comment","#{comment}");
                VALUES("create_time", "#{createTime}");
                VALUES("create_user_id", "#{createUserId}");
                VALUES("update_time", "#{updateTime}");
                VALUES("update_user_id", "#{updateUserId}");
            }}.toString();
            return sql;
        }

        public String update(BCustomerAutoFreightAgreementPojo autoAgreementPojo){
            String sql= new SQL() {{
                UPDATE("b_customer_auto_freight_agreement t");
                SET("t.customer_id = #{customerId}");
                SET("t.city_id = #{cityId}");
                SET("t.comment = #{comment}");
                SET("t.update_time = #{updateTime}");
                SET("t.update_user_id = #{updateUserId}");
                WHERE("t.id = #{id}");
            }}.toString();
            return sql;
        }

        public String deleteByIds(String agreementFreightIds){
            String sql = new SQL() {{
                DELETE_FROM("b_customer_auto_freight_agreement t");
                WHERE("t.id in (" + agreementFreightIds + ")");
            }}.toString();
            return sql;
        }

    }

    @Results(id = "bCustomerAutoFreightAgreementMapId",value = {
            @Result(column = "id", property = "id"),
            @Result(column = "customer_id", property = "customerId"),
            @Result(column = "city_id", property = "cityId"),
            @Result(column = "file_id", property = "fileId"),
            @Result(column = "comment", property = "comment"),
            @Result(column = "create_time", property = "createTime"),
            @Result(column = "create_user_id", property = "createUserId"),
            @Result(column = "update_time", property = "updateTime"),
            @Result(column = "update_user_id", property = "updateUserId")
    })
    @SelectProvider(type = BCustomerAutoFreightAgreementMapperProvider.class, method = "findAll")
    List<BCustomerAutoFreightAgreementPojo> findAll(Map param);

    @ResultMap("bCustomerAutoFreightAgreementMapId")
    @SelectProvider(type = BCustomerAutoFreightAgreementMapperProvider.class, method = "getAutoFreightAgreementPojoByIds")
    List<BCustomerAutoFreightAgreementPojo> getAutoFreightAgreementPojoByIds(String agreementFreightIds);

    @ResultMap("bCustomerAutoFreightAgreementMapId")
    @SelectProvider(type = BCustomerAutoFreightAgreementMapperProvider.class, method = "getAutoFreightAgreementPojo")
    BCustomerAutoFreightAgreementPojo getAutoFreightAgreementPojo(BCustomerAutoFreightAgreementPojo autoAgreementPojo);

    @InsertProvider(type = BCustomerAutoFreightAgreementMapperProvider.class, method = "insert")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(BCustomerAutoFreightAgreementPojo autoAgreementPojo);

    @UpdateProvider(type = BCustomerAutoFreightAgreementMapperProvider.class, method = "update")
    int update(BCustomerAutoFreightAgreementPojo autoAgreementPojo);

    @DeleteProvider(type = BCustomerAutoFreightAgreementMapperProvider.class, method = "deleteByIds")
    int deleteByIds(String agreementFreightIds);

}
